/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package clientprogram;

import Command.CommandStatement;
import Command.Constant_Command;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ASUS
 */
public class User {
    public static final String ANONYMOUS = "Anonymous";
    public String username = ANONYMOUS;
    public String password = ANONYMOUS;
    public String userId;
    public int minRemain;
    public int pricePerHour = 3000;
    public int money = 0;
    public long totalTimeUsed = 0;
    public String startTime = null;
    private static User instance = null;
    public static User Instance(){
        if (instance == null){
            instance = new User();
        }
        return instance;
    }
    
    public User(){
       DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       Date date = new Date();
       startTime = dateFormat.format(date);
    }
    
    public boolean Recharge(int _sotien)
    {
        SocketCommunication.Instance().SendCommand(new CommandStatement(Constant_Command.CARD_REQUEST, String.valueOf(_sotien)));
        return true;
    }
    
    public void SetTotalUsedTime(long _min){
        totalTimeUsed = _min;
        money = (int) ((float)totalTimeUsed / 60 * pricePerHour);
    }
    
    public void ChangePass(String _oldPass, String _newPass)
    {
        SocketCommunication.Instance().SendCommand(new CommandStatement(Constant_Command.CHANGE_PASSWORD, _oldPass, _newPass));
    }
    
    public void CheckLogin(String _username, String _pass){
        username = _username;
        password = _pass;
        System.out.println("Sending check login...");
        SocketCommunication.Instance().SendCommand(new CommandStatement(Constant_Command.LOGIN, _username, _pass));
        System.out.println("Sended check login...");
    }
    
    public void Logout()
    {
        if (!username.equals(ANONYMOUS)){
            minRemain -= totalTimeUsed;
        }
        SocketCommunication.Instance().SendCommand(new CommandStatement(Constant_Command.TURN_OFF_SERVICE, String.valueOf(minRemain)));
        username = ANONYMOUS;
        password = ANONYMOUS;
        try {
             LockScreen.Instance().Open();
         } catch (IOException ex) {
             Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
         }
    }
}
